//
//  HYFStatusCell.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/24.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit
import SnapKit

//cell 中使用的间距
let statusCellMargin:CGFloat = 10

class HYFStatusCell: UITableViewCell {
    
    //记录底部视图的顶部约束
    var toolBarBottomConstraint: Constraint?

    //定义一个 viewModel
    var statusViewModel:HYFStatusViewModel?{
        
        didSet{
           //给原创微博赋值
            originalView.statusViewModel = statusViewModel
            
            //首先卸载 底部视图的顶部约束
            self.toolBarBottomConstraint?.uninstall()
            
            //没有转发微博
            if statusViewModel?.status?.retweeted_status == nil {
                
                toolBarView.snp_updateConstraints(closure: { (make) in
                    
                self.toolBarBottomConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                    
                })
                
                //隐藏 retweetView
                retweetView.hidden = true
                
            }else{
                
                //先赋值
                retweetView.statusViewModel = statusViewModel
                
                //有转发微博
                toolBarView.snp_updateConstraints(closure: { (make) in
                    
                  self.toolBarBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
                
                //显示出来
                retweetView.hidden = false
                
                //给底部视图的 model 赋值
                self.toolBarView.statusViewModel = statusViewModel
            }
        }
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func setupUI(){
        
        backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(retweetView)
        
        //增加约束
        originalView.snp_makeConstraints { (make) in
            
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
        }
        
        //底部工具视图
        toolBarView.snp_makeConstraints { (make) in
            
            //给 属性约束赋值
            self.toolBarBottomConstraint =  make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //设置contentView 的约束
        contentView.snp_makeConstraints { (make) in
         
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
        
        retweetView.snp_makeConstraints { (make) in
            
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
        }
        
    }
    
    //懒加载控件
    private lazy var originalView:HYFStatusOriginalView = {
        
       let view = HYFStatusOriginalView()
        view.backgroundColor = RandomColor()
        
        return view
    }()
    
    private lazy var toolBarView:HYFStatusToolBarView = HYFStatusToolBarView()
    
    private lazy var retweetView:HYFStatusRetweetView = HYFStatusRetweetView()
    
}
